<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Java8到Java17之间的主要特性描述 | 西瓜蓬蓬的静谧庭院</title><meta name="keywords" content="java,lambda,stream,module,exports,requires,transitive,record,sealed,permits"><meta name="author" content="xiguapengpeng"><meta name="copyright" content="xiguapengpeng"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Java8到Java17之间的主要特性描述[TOC] Java8主要特性：  lambda表达式与Stream API（Lambda Expression and Stream API） 方法引用（Method Reference） 接口默认方法（Default Methods） 类型注解（Type Annotations） 可重复注解（Repeating Annotations） 方法参数反射（">
<meta property="og:type" content="article">
<meta property="og:title" content="Java8到Java17之间的主要特性描述">
<meta property="og:url" content="https://xiguapengpeng.gitee.io/posts/fdc8614/index.html">
<meta property="og:site_name" content="西瓜蓬蓬的静谧庭院">
<meta property="og:description" content="Java8到Java17之间的主要特性描述[TOC] Java8主要特性：  lambda表达式与Stream API（Lambda Expression and Stream API） 方法引用（Method Reference） 接口默认方法（Default Methods） 类型注解（Type Annotations） 可重复注解（Repeating Annotations） 方法参数反射（">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230130095755.png">
<meta property="article:published_time" content="2023-01-30T01:59:04.252Z">
<meta property="article:modified_time" content="2023-01-30T05:49:40.853Z">
<meta property="article:author" content="xiguapengpeng">
<meta property="article:tag" content="java">
<meta property="article:tag" content="lambda">
<meta property="article:tag" content="stream">
<meta property="article:tag" content="module">
<meta property="article:tag" content="exports">
<meta property="article:tag" content="requires">
<meta property="article:tag" content="transitive">
<meta property="article:tag" content="record">
<meta property="article:tag" content="sealed">
<meta property="article:tag" content="permits">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230130095755.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://xiguapengpeng.gitee.io/posts/fdc8614/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="CMfiiaCCwQ90-u9hmlL-7VUq-v_zt9rhyg1Ke1A7ia4"/><meta name="baidu-site-verification" content="code-Wsugu6NsqF"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?0b83b38ff142c9faea5f5987d7c6f3f0";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Java8到Java17之间的主要特性描述',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-01-30 13:49:40'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.0.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://gitee.com/xiguapengpeng/imgtu/raw/master/blog/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">34</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">122</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-compass"></i><span> 导航</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 豆瓣</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li><li><a class="site-page child" href="/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230130095755.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">西瓜蓬蓬的静谧庭院</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-compass"></i><span> 导航</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 豆瓣</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li><li><a class="site-page child" href="/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Java8到Java17之间的主要特性描述</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-01-30T01:59:04.252Z" title="发表于 2023-01-30 09:59:04">2023-01-30</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-01-30T05:49:40.853Z" title="更新于 2023-01-30 13:49:40">2023-01-30</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/backend/">backend</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>14分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Java8到Java17之间的主要特性描述"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Java8到Java17之间的主要特性描述"><a href="#Java8到Java17之间的主要特性描述" class="headerlink" title="Java8到Java17之间的主要特性描述"></a>Java8到Java17之间的主要特性描述</h1><p>[TOC]</p>
<h2 id="Java8"><a href="#Java8" class="headerlink" title="Java8"></a>Java8</h2><p>主要特性：</p>
<ol>
<li>lambda表达式与Stream API（Lambda Expression and Stream API）</li>
<li>方法引用（Method Reference）</li>
<li>接口默认方法（Default Methods）</li>
<li>类型注解（Type Annotations）</li>
<li>可重复注解（Repeating Annotations）</li>
<li>方法参数反射（Method Parameter Reflection）</li>
</ol>
<h3 id="lambda表达式与Stream-API"><a href="#lambda表达式与Stream-API" class="headerlink" title="lambda表达式与Stream API"></a>lambda表达式与Stream API</h3><p>一般集合数据的处理示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> List&lt;Car&gt; <span class="title function_">findCheapCar</span><span class="params">(List&lt;Car&gt; cars)</span> &#123;</span><br><span class="line">    List&lt;Car&gt; result = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">    <span class="keyword">for</span> (Car car : cars) &#123;</span><br><span class="line">        <span class="keyword">if</span> (car.getPrice() &lt; <span class="number">50000</span>) &#123;</span><br><span class="line">            result.add(car);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>lambda表达式与Stream API</strong>搭配的示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> List&lt;Car&gt; <span class="title function_">findCheapCar</span><span class="params">(List&lt;Car&gt; cars)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> cars.stream().filter(car -&gt; car.getPrice() &lt; <span class="number">50000</span>)</span><br><span class="line">		.collect(Collectors.toList());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>通过调用stream()方法将集合类型的cars变量转换为stream，内部通过filter()方法设定对应的过滤条件，最后通过colletct()方法将最后的结果集包装为List返回。</p>
<h3 id="方法引用（Method-Reference）"><a href="#方法引用（Method-Reference）" class="headerlink" title="方法引用（Method Reference）"></a>方法引用（Method Reference）</h3><p>一般lambda中引用方法示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> List&lt;String&gt; <span class="title function_">mapCarName</span><span class="params">(List&lt;Car&gt; cars)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> cars.stream().map(car -&gt; car.getName())</span><br><span class="line">		.collect(Collectors.toList());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>方法引用</strong>使得代码更加简洁并且可读性更好，使用两个冒号<code>::</code>来引用方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> List&lt;String&gt; <span class="title function_">mapCarName</span><span class="params">(List&lt;Car&gt; cars)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> cars.stream().map(Car::toString)</span><br><span class="line">		.collect(Collectors.toList());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="接口默认方法（Default-Methods）"><a href="#接口默认方法（Default-Methods）" class="headerlink" title="接口默认方法（Default Methods）"></a>接口默认方法（Default Methods）</h3><p>当一个接口新增了方法，又不希望调用方端有任何感知。这时候，就可以使用接口默认方法（Default Methods）</p>
<p>来实现这个功能。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">IElectricCar</span> &#123;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">charge</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BYD</span> <span class="keyword">implements</span> <span class="title class_">IElectricCar</span> &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">charge</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;比亚迪开始充电了&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>添加一个新方法到接口中：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">IElectricCar</span> &#123;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">charge</span><span class="params">()</span>;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">charge</span><span class="params">(Date date)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这时候IElectricCar接口所有实现类都会出现编译报错：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Class <span class="string">&#x27;BYD&#x27;</span> must either be declared abstract </span><br><span class="line">or implement abstract method <span class="string">&#x27;charge(Date)&#x27;</span> <span class="keyword">in</span> <span class="string">&#x27;IElectricCar&#x27;</span></span><br></pre></td></tr></table></figure>

<p><strong>接口默认方法</strong>可以使用关键词<code>default</code>在接口中默认实现方法解决这个问题：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">IElectricCar</span> &#123;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">charge</span><span class="params">()</span>;</span><br><span class="line">    <span class="keyword">default</span> <span class="keyword">void</span> <span class="title function_">charge</span><span class="params">(Date date)</span> &#123;</span><br><span class="line">        System.out.println(date.toString() + <span class="string">&quot;：比亚迪开始充电了&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="类型注解（Type-Annotations）"><a href="#类型注解（Type-Annotations）" class="headerlink" title="类型注解（Type Annotations）"></a>类型注解（Type Annotations）</h3><p>Field.getAnnotations()是用来获取Target为ElementType.FIELD的注解；</p>
<p>Field.getAnnotatedType().getAnnotations()用来获取Target为ElementType.TYPE_USE的注解，即类型注解。</p>
<p><strong>类型注解</strong>可以在使用类型的任意地方使用它们，使用场景包括但不限于：</p>
<ul>
<li>本地变量定义</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    <span class="meta">@NotNull</span> <span class="type">String</span> <span class="variable">userName</span> <span class="operator">=</span> args[<span class="number">0</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>构造器调用</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">	List&lt;String&gt; request = <span class="keyword">new</span> <span class="meta">@NotEmpty</span> ArrayList&lt;&gt;(Arrays.stream(args)</span><br><span class="line">		.collect(Collectors.toList()));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>泛型</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    List&lt;<span class="meta">@Email</span> String&gt; emails;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="可重复注解（Repeating-Annotations）"><a href="#可重复注解（Repeating-Annotations）" class="headerlink" title="可重复注解（Repeating Annotations）"></a>可重复注解（Repeating Annotations）</h3><p>创建<strong>可重复注解</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Repeatable(Notifications.class)</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> Notify &#123;</span><br><span class="line">    String <span class="title function_">phone</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> Notifications &#123;</span><br><span class="line">    Notify[] value();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在普通注解@Notify上增加了@Repeatable元注解，说明将@Notify注解当做@Notifications注解的对象数组元素来使用。</p>
<p>使用<strong>可重复注解</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Notify(phone = &quot;15880000000&quot;)</span></span><br><span class="line"><span class="meta">@Notify(phone = &quot;18900000000&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">expressTimeoutException</span> <span class="keyword">extends</span> <span class="title class_">RuntimeException</span> &#123;</span><br><span class="line">    <span class="keyword">final</span> String username;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">expressTimeoutException</span><span class="params">(String username)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.username = username;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Java9"><a href="#Java9" class="headerlink" title="Java9"></a>Java9</h2><p>主要特性：</p>
<ol>
<li>Java模块化（Java Module System）</li>
<li>带有内部匿名类的菱形语法（Diamond Syntax with Inner Anonymous Classes）</li>
<li>私有接口方法（Private Interface Methods）</li>
</ol>
<h3 id="Java模块化（Java-Module-System）"><a href="#Java模块化（Java-Module-System）" class="headerlink" title="Java模块化（Java Module System）"></a>Java模块化（Java Module System）</h3><p><strong>模块化</strong>是一组包（package）以及包的依赖与资源，相对于包而言，它提供了更加广泛的功能。</p>
<p>模块的是通过<code>module-info.java</code>进行定义，编译后打包后，就成为一个模块的实体；在模块的定义文件中，我</p>
<p>们需要指定模块之间的依赖靠关系，可以<strong>exports</strong>给那些模块用，需要使用那些模块<strong>requires</strong>。</p>
<p>模块声明文件通常放置在模块源码的根目录：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># 声明</span><br><span class="line"><span class="keyword">module</span> com.foo.bar &#123;</span><br><span class="line">    <span class="keyword">requires</span> org.baz.qux;</span><br><span class="line">	<span class="keyword">exports</span> com.foo.bar.alpha;</span><br><span class="line">	<span class="keyword">exports</span> com.foo.bar.beta;</span><br><span class="line">&#125;</span><br><span class="line"># 位置</span><br><span class="line"><span class="keyword">module</span>-info.java</span><br><span class="line">com/foo/bar/alpha/AlphaFactory.java</span><br><span class="line">com/foo/bar/alpha/Alpha.java</span><br></pre></td></tr></table></figure>

<p>模块之间的关系被称作可读性（readability），代表一个模块是否可以找到这个模块文件，并且读入系统中（注</p>
<p>意：并非代表可以访问其中的类型）。在实际的代码，一个类型对于另外一个类型的调用，我们称之为可访问性</p>
<p>（Accessible），这意味着可以使用这个类型；可访问性的前提是可读性，换句话说，现有模块可读，然后再进</p>
<p>一步检测可访问性（安全）。在Java9中，Public不再意味着任意的可访问性。</p>
<p><strong>transitive</strong>可以可以提供一个间接可读性：</p>
<p><img src="http://xiguapengpeng.gitee.io/imgtu/blog/post_img_20230128172552.jpg" alt="间接引用"></p>
<h3 id="带有内部匿名类的菱形语法（Diamond-Syntax-with-Inner-Anonymous-Classes）"><a href="#带有内部匿名类的菱形语法（Diamond-Syntax-with-Inner-Anonymous-Classes）" class="headerlink" title="带有内部匿名类的菱形语法（Diamond Syntax with Inner Anonymous Classes）"></a>带有内部匿名类的菱形语法（Diamond Syntax with Inner Anonymous Classes）</h3><p>在Java9之前不能在匿名内部类中使用<code>&lt;&gt;</code>操作符，如下面代码示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">abstract</span> <span class="keyword">class</span> <span class="title class_">StringAppender</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">abstract</span> T <span class="title function_">append</span><span class="params">(String a, String b)</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">	StringAppender&lt;String&gt; appending = <span class="keyword">new</span> <span class="title class_">StringAppender</span>&lt;&gt;() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> String <span class="title function_">append</span><span class="params">(String a, String b)</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(a).append(<span class="string">&quot;-&quot;</span>).append(b).toString();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在Java8中，会出现编译错误：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&#x27;&lt;&gt;&#x27;</span> cannot be used with anonymous classes</span><br><span class="line"><span class="comment">#无法推断StringAppender&lt;T&gt;的泛型，必须指定&lt;String&gt;</span></span><br></pre></td></tr></table></figure>

<p>将环境切换为Java9能正确编译通过。</p>
<h3 id="私有接口方法（Private-Interface-Methods）"><a href="#私有接口方法（Private-Interface-Methods）" class="headerlink" title="私有接口方法（Private Interface Methods）"></a>私有接口方法（Private Interface Methods）</h3><p>在Java9中可以在接口中定义私有方法，私有方法的主要目的在于通用方法的封装以提升其在别的默认方法中的可复用性。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    <span class="type">TestingNames</span> <span class="variable">names</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">TestingNames</span>();</span><br><span class="line">    System.out.println(names.fetchInitialData());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">class</span> <span class="title class_">TestingNames</span> <span class="keyword">implements</span> <span class="title class_">NamesInterface</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">TestingNames</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">NamesInterface</span> &#123;</span><br><span class="line">    <span class="keyword">default</span> List&lt;String&gt; <span class="title function_">fetchInitialData</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">try</span> (<span class="type">BufferedReader</span> <span class="variable">br</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedReader</span>(</span><br><span class="line">            <span class="keyword">new</span> <span class="title class_">InputStreamReader</span>(<span class="built_in">this</span>.getClass().getResourceAsStream(<span class="string">&quot;/names.txt&quot;</span>)))) &#123;</span><br><span class="line">            <span class="keyword">return</span> readNames(br);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> List&lt;String&gt; <span class="title function_">readNames</span><span class="params">(BufferedReader br)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        ArrayList&lt;String&gt; names = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">while</span> ((name = br.readLine()) != <span class="literal">null</span>) &#123;</span><br><span class="line">            names.add(name);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> names;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Java10"><a href="#Java10" class="headerlink" title="Java10"></a>Java10</h2><h3 id="局部变量类型推断（Local-Variable-Type-Inference）"><a href="#局部变量类型推断（Local-Variable-Type-Inference）" class="headerlink" title="局部变量类型推断（Local Variable Type Inference）"></a>局部变量类型推断（Local Variable Type Inference）</h3><p>Java需要在局部变量中显式地定义类型，代码显得非常冗余。Java10中的var类型允许省略语句左侧的类型声明，使得语句干练简洁。</p>
<p>Java10前的旧写法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    <span class="type">Person</span> <span class="variable">Roland</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Roland&quot;</span>, <span class="string">&quot;Deschain&quot;</span>);</span><br><span class="line">    <span class="type">Person</span> <span class="variable">Susan</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Susan&quot;</span>, <span class="string">&quot;Delgado&quot;</span>);</span><br><span class="line">    <span class="type">Person</span> <span class="variable">Eddie</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Eddie&quot;</span>, <span class="string">&quot;Dean&quot;</span>);</span><br><span class="line">    <span class="type">Person</span> <span class="variable">Detta</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Detta&quot;</span>, <span class="string">&quot;Walker&quot;</span>);</span><br><span class="line">    <span class="type">Person</span> <span class="variable">Jake</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Jake&quot;</span>, <span class="string">&quot;Chambers&quot;</span>);</span><br><span class="line"></span><br><span class="line">    List&lt;Person&gt; persons = List.of(Roland, Susan, Eddie, Detta, Jake);</span><br><span class="line">    <span class="keyword">for</span> (Person person : persons) &#123;</span><br><span class="line">        System.out.println(person.name + <span class="string">&quot; - &quot;</span> + person.lastname);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用<code>var</code>后的写法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    <span class="type">var</span> <span class="variable">Roland</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Roland&quot;</span>, <span class="string">&quot;Deschain&quot;</span>);</span><br><span class="line">    <span class="type">var</span> <span class="variable">Susan</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Susan&quot;</span>, <span class="string">&quot;Delgado&quot;</span>);</span><br><span class="line">    <span class="type">var</span> <span class="variable">Eddie</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Eddie&quot;</span>, <span class="string">&quot;Dean&quot;</span>);</span><br><span class="line">    <span class="type">var</span> <span class="variable">Detta</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Detta&quot;</span>, <span class="string">&quot;Walker&quot;</span>);</span><br><span class="line">    <span class="type">var</span> <span class="variable">Jake</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Jake&quot;</span>, <span class="string">&quot;Chambers&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="type">var</span> <span class="variable">persons</span> <span class="operator">=</span> List.of(Roland, Susan, Eddie, Detta, Jake);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> person : persons) &#123;</span><br><span class="line">        System.out.println(person.name + <span class="string">&quot; - &quot;</span> + person.lastname);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Java11"><a href="#Java11" class="headerlink" title="Java11"></a>Java11</h2><h3 id="Lambda表达式中的局部变量类型推断（Local-Variable-Type-in-Lambda-Expressions）"><a href="#Lambda表达式中的局部变量类型推断（Local-Variable-Type-in-Lambda-Expressions）" class="headerlink" title="Lambda表达式中的局部变量类型推断（Local Variable Type in Lambda Expressions）"></a>Lambda表达式中的局部变量类型推断（Local Variable Type in Lambda Expressions）</h3><p>在Java11中针对Java10中的局部变量类型推断这个特性进行了改善，可以在lambda表达式中使用var进行类型推断而不是进行明确的类型声明。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    <span class="type">var</span> <span class="variable">Roland</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Roland&quot;</span>, <span class="string">&quot;Deschain&quot;</span>);</span><br><span class="line">    <span class="type">var</span> <span class="variable">Susan</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Susan&quot;</span>, <span class="string">&quot;Delgado&quot;</span>);</span><br><span class="line">    <span class="type">var</span> <span class="variable">Eddie</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Eddie&quot;</span>, <span class="string">&quot;Dean&quot;</span>);</span><br><span class="line">    <span class="type">var</span> <span class="variable">Detta</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Detta&quot;</span>, <span class="string">&quot;Walker&quot;</span>);</span><br><span class="line">    <span class="type">var</span> <span class="variable">Jake</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;Jake&quot;</span>, <span class="string">&quot;Chambers&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="type">var</span> <span class="variable">filteredPersons</span> <span class="operator">=</span> List.of(Roland, Susan, Eddie, Detta, Jake).stream()</span><br><span class="line">        .filter((<span class="keyword">var</span> x) -&gt; x.name.contains(<span class="string">&quot;a&quot;</span>))</span><br><span class="line">        .collect(Collectors.toList());</span><br><span class="line">    System.out.println(filteredPersons);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Java14"><a href="#Java14" class="headerlink" title="Java14"></a>Java14</h2><h3 id="Switch表达式（Switch-Expressions）"><a href="#Switch表达式（Switch-Expressions）" class="headerlink" title="Switch表达式（Switch Expressions）"></a>Switch表达式（Switch Expressions）</h3><p>Java14前的旧写法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">days</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">    <span class="type">Month</span> <span class="variable">month</span> <span class="operator">=</span> Month.APRIL;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">switch</span> (month) &#123;</span><br><span class="line">        <span class="keyword">case</span> JANUARY:</span><br><span class="line">        <span class="keyword">case</span> MARCH:</span><br><span class="line">        <span class="keyword">case</span> MAY:</span><br><span class="line">        <span class="keyword">case</span> JULY:</span><br><span class="line">        <span class="keyword">case</span> AUGUST:</span><br><span class="line">        <span class="keyword">case</span> OCTOBER:</span><br><span class="line">        <span class="keyword">case</span> DECEMBER:</span><br><span class="line">            days = <span class="number">31</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> FEBRUARY :</span><br><span class="line">            days = <span class="number">28</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> APRIL:</span><br><span class="line">        <span class="keyword">case</span> JUNE:</span><br><span class="line">        <span class="keyword">case</span> SEPTEMBER:</span><br><span class="line">        <span class="keyword">case</span> NOVEMBER:</span><br><span class="line">            days = <span class="number">30</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Java14的新写法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">days</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">    <span class="type">Month</span> <span class="variable">month</span> <span class="operator">=</span> Month.APRIL;</span><br><span class="line"></span><br><span class="line">    days = <span class="keyword">switch</span> (month) &#123;</span><br><span class="line">        <span class="keyword">case</span> JANUARY, MARCH, MAY, JULY, AUGUST, OCTOBER, DECEMBER :</span><br><span class="line">            days = <span class="number">31</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> FEBRUARY :</span><br><span class="line">            days = <span class="number">28</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> APRIL, JUNE, SEPTEMBER, NOVEMBER :</span><br><span class="line">            days = <span class="number">30</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用箭头标识符（-&gt;）改造后的写法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">days</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">    <span class="type">Month</span> <span class="variable">month</span> <span class="operator">=</span> Month.APRIL;</span><br><span class="line"></span><br><span class="line">    days = <span class="keyword">switch</span> (month) &#123;</span><br><span class="line">        <span class="keyword">case</span> JANUARY, MARCH, MAY, JULY, AUGUST, OCTOBER, DECEMBER -&gt; <span class="number">31</span>;</span><br><span class="line">        <span class="keyword">case</span> FEBRUARY -&gt; <span class="number">28</span>;</span><br><span class="line">        <span class="keyword">case</span> APRIL, JUNE, SEPTEMBER, NOVEMBER -&gt; <span class="number">30</span>;</span><br><span class="line">        <span class="keyword">default</span> -&gt; <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>();</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在case块中，java14使用了箭头标识符（-&gt;）替代冒号（:），即使没有break关键字，也会在匹配到的第一个case分支后跳出switch语句。</p>
<p><code>yield</code>关键字配合箭头标识符（-&gt;）可以处理更复杂逻辑：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">days</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">    <span class="type">Month</span> <span class="variable">month</span> <span class="operator">=</span> Month.APRIL;</span><br><span class="line"></span><br><span class="line">    days = <span class="keyword">switch</span> (month) &#123;</span><br><span class="line">        <span class="keyword">case</span> JANUARY, MARCH, MAY, JULY, AUGUST, OCTOBER, DECEMBER -&gt; &#123;</span><br><span class="line">            System.out.println(month);</span><br><span class="line">            yield <span class="number">31</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">case</span> FEBRUARY -&gt; &#123;</span><br><span class="line">            System.out.println(month);</span><br><span class="line">            yield <span class="number">28</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">case</span> APRIL, JUNE, SEPTEMBER, NOVEMBER -&gt; &#123;</span><br><span class="line">            System.out.println(month);</span><br><span class="line">            yield <span class="number">30</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">default</span> -&gt; <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>();</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Java15"><a href="#Java15" class="headerlink" title="Java15"></a>Java15</h2><h3 id="文本块（Text-Blocks）"><a href="#文本块（Text-Blocks）" class="headerlink" title="文本块（Text Blocks）"></a>文本块（Text Blocks）</h3><p>文本块可以理解为对格式化字符串的一个优化操作，从Java15允许编写一个跨越多行的字符串作为常规文本。相</p>
<p>比于在Java15之前，跨多行需要使用”+”号进行多行字符串拼接，会更加的方便并且可读性更高。</p>
<p>Java15之前，需要人为的保证字符串的格式化操作：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    System.out.println(</span><br><span class="line">        <span class="string">&quot;&lt;!DOCTYPE html&gt;\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;&lt;html&gt;\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;     &lt;head&gt;\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;        &lt;title&gt;Example&lt;/title&gt;\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;    &lt;/head&gt;\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;    &lt;body&gt;\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;        &lt;p&gt;This is an example of a simple HTML &quot;</span> +</span><br><span class="line">        <span class="string">&quot;		page with one paragraph.&lt;/p&gt;\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;    &lt;/body&gt;\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;&lt;/html&gt;\n&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Java15使用Text Blocks之后：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    System.out.println(</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        &lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="string">        &lt;html&gt;</span></span><br><span class="line"><span class="string">        	&lt;head&gt;</span></span><br><span class="line"><span class="string">        		&lt;title&gt;Example&lt;/title&gt;</span></span><br><span class="line"><span class="string">        	&lt;/head&gt;</span></span><br><span class="line"><span class="string">            &lt;body&gt;</span></span><br><span class="line"><span class="string">            	&lt;p&gt;This is an example of a simple HTML </span></span><br><span class="line"><span class="string">            	page with one paragraph.&lt;/p&gt;</span></span><br><span class="line"><span class="string">            &lt;/body&gt;</span></span><br><span class="line"><span class="string">        &lt;/html&gt;      </span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">    );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Java16"><a href="#Java16" class="headerlink" title="Java16"></a>Java16</h2><h3 id="instanceof-模式匹配（Pattern-Matching-of-instanceof）"><a href="#instanceof-模式匹配（Pattern-Matching-of-instanceof）" class="headerlink" title="instanceof 模式匹配（Pattern Matching of instanceof）"></a>instanceof 模式匹配（Pattern Matching of instanceof）</h3><p>instanceof模式匹配允许我们以内联形式转换变量，并在所需的if-else块中使用它而无需进行强转。</p>
<p>需要强转的旧写法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">double</span> <span class="title function_">getPrice</span><span class="params">(Car car)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (car <span class="keyword">instanceof</span> BYD) &#123;</span><br><span class="line">        <span class="type">BYD</span> <span class="variable">byd</span> <span class="operator">=</span> (BYD) car;</span><br><span class="line">        <span class="keyword">return</span> byd.getPrice();</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (car <span class="keyword">instanceof</span> Tesla) &#123;</span><br><span class="line">        <span class="type">Tesla</span> <span class="variable">tesla</span> <span class="operator">=</span> (Tesla) car;</span><br><span class="line">        <span class="keyword">return</span> tesla.getPrice();</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalArgumentException</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用模式匹配写法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">double</span> <span class="title function_">getPrice</span><span class="params">(Car car)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (car <span class="keyword">instanceof</span> BYD byd) &#123;</span><br><span class="line">        <span class="keyword">return</span> byd.getPrice();</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (car <span class="keyword">instanceof</span> Tesla tesla) &#123;</span><br><span class="line">        <span class="keyword">return</span> tesla.getPrice();</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalArgumentException</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>需要注意的是，对应的变量范围，只在当前所属的if-else代码块中可见。</p>
<h3 id="档案类（Records）"><a href="#档案类（Records）" class="headerlink" title="档案类（Records）"></a>档案类（Records）</h3><p>档案类是用来表示<strong>不可变数据的透明载体</strong>，用来简化不可变数据的表达，提高编码效率，降低编码错误。</p>
<ul>
<li>档案类是个终极（final）类，其父类是父类是java.lang.Record。不支持继承，不能用任何其他类来继承它。没有子类，也就意味着我们不能通过修改子类来改变档案类；</li>
<li>档案类允许实现接口；</li>
<li>档案类声明的变量是不可变的变量；</li>
<li>档案类不能声明可变的变量，也不能支持实例初始化的方法。这就保证了，我们只能使用档案类形式的构造方法，避免额外的初始化对可变性的影响；</li>
<li>档案类不能声明本地（native）方法。如果允许了本地方法，也就意味着打开了修改不可变变量的后门。</li>
</ul>
<p>老式的POJO定义：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Car</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">double</span> price;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">double</span> <span class="title function_">getPrice</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> price;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setPrice</span><span class="params">(<span class="type">double</span> price)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.price = price;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Car</span><span class="params">(String name, String price)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.price = price;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object o)</span> &#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">hashCode</span><span class="params">()</span> &#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在类名前使用关键词<code>record</code>定义为档案类，一行解决：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">record</span> <span class="title class_">CarRecord</span><span class="params">(String name, <span class="type">double</span> price)</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Java17"><a href="#Java17" class="headerlink" title="Java17"></a>Java17</h2><h3 id="封闭类（Sealed-Classes）"><a href="#封闭类（Sealed-Classes）" class="headerlink" title="封闭类（Sealed Classes）"></a>封闭类（Sealed Classes）</h3><p>类的final修饰符能禁止任何类继承它，Java17封闭类带来的全新特性，封闭类允许针对permit指定的类进行继承</p>
<p>扩展，除此之外，封闭类对于其它类而言，可以理解为是带有final修饰符的类。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">sealed</span> <span class="keyword">class</span> <span class="title class_">Car</span> permits BYD, Tesla &#123;&#125;</span><br></pre></td></tr></table></figure>

<p>permits关键字指定的子类必须要增加如下几种修饰符中的一种：</p>
<ul>
<li>final：表示该类不能被继承</li>
<li>sealed：修饰类&#x2F;接口，用来描述这个类&#x2F;接口为密封类&#x2F;接口</li>
<li>non-sealed：修饰类&#x2F;接口，用来描述这个类&#x2F;接口为非密封类&#x2F;接口，显式放弃密封</li>
<li>permits：用在extends和implements之后，指定可以继承或实现的类</li>
</ul>
<p>封闭类会带来相应的条件约束，包括：</p>
<ul>
<li>Permitted 子类在编译期必须能够被封闭类访问</li>
<li>Permitted 子类必须直接继承封闭类</li>
<li>Permitted 子类必须有如下修饰符中的一个进行修饰：final，sealed，non-sealed</li>
<li>Permitted 子类必须在同一个java模块中</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">class</span> <span class="title class_">BYD</span> <span class="keyword">extends</span> <span class="title class_">Car</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">xiguapengpeng</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://xiguapengpeng.gitee.io/posts/fdc8614/">https://xiguapengpeng.gitee.io/posts/fdc8614/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://xiguapengpeng.gitee.io" target="_blank">西瓜蓬蓬的静谧庭院</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/java/">java</a><a class="post-meta__tags" href="/tags/lambda/">lambda</a><a class="post-meta__tags" href="/tags/stream/">stream</a><a class="post-meta__tags" href="/tags/module/">module</a><a class="post-meta__tags" href="/tags/exports/">exports</a><a class="post-meta__tags" href="/tags/requires/">requires</a><a class="post-meta__tags" href="/tags/transitive/">transitive</a><a class="post-meta__tags" href="/tags/record/">record</a><a class="post-meta__tags" href="/tags/sealed/">sealed</a><a class="post-meta__tags" href="/tags/permits/">permits</a></div><div class="post_share"><div class="social-share" data-image="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230130095755.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://gitee.com/xiguapengpeng/imgtu/raw/master/blog/wx_reward.jpg" target="_blank"><img class="post-qr-code-img" src="https://gitee.com/xiguapengpeng/imgtu/raw/master/blog/wx_reward.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/bd6c95d5/"><img class="prev-cover" src="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230131095714.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Windows10安装java环境</div></div></a></div><div class="next-post pull-right"><a href="/posts/9542a1f7/"><img class="next-cover" src="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230114172144.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">CentOS7安装OpenSSL环境</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/b3dd0b9e/" title="CentOS7安装jdk环境"><img class="cover" src="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_1644828629.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-18</div><div class="title">CentOS7安装jdk环境</div></div></a></div><div><a href="/posts/785834c2/" title="RabbitMQ Management HTTP API的简单封装"><img class="cover" src="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20220512154924.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-12</div><div class="title">RabbitMQ Management HTTP API的简单封装</div></div></a></div><div><a href="/posts/bd6c95d5/" title="Windows10安装java环境"><img class="cover" src="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230131095714.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-01-31</div><div class="title">Windows10安装java环境</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://gitee.com/xiguapengpeng/imgtu/raw/master/blog/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">xiguapengpeng</div><div class="author-info__description">分享知识、勤做笔记</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">34</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">122</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xiguapengpeng"><i class="fab fa-heart"></i><span>个人仓库</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">若时光逝却，就珍存过往</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Java8%E5%88%B0Java17%E4%B9%8B%E9%97%B4%E7%9A%84%E4%B8%BB%E8%A6%81%E7%89%B9%E6%80%A7%E6%8F%8F%E8%BF%B0"><span class="toc-number">1.</span> <span class="toc-text">Java8到Java17之间的主要特性描述</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Java8"><span class="toc-number">1.1.</span> <span class="toc-text">Java8</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%B8%8EStream-API"><span class="toc-number">1.1.1.</span> <span class="toc-text">lambda表达式与Stream API</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E5%BC%95%E7%94%A8%EF%BC%88Method-Reference%EF%BC%89"><span class="toc-number">1.1.2.</span> <span class="toc-text">方法引用（Method Reference）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8E%A5%E5%8F%A3%E9%BB%98%E8%AE%A4%E6%96%B9%E6%B3%95%EF%BC%88Default-Methods%EF%BC%89"><span class="toc-number">1.1.3.</span> <span class="toc-text">接口默认方法（Default Methods）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E6%B3%A8%E8%A7%A3%EF%BC%88Type-Annotations%EF%BC%89"><span class="toc-number">1.1.4.</span> <span class="toc-text">类型注解（Type Annotations）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%AF%E9%87%8D%E5%A4%8D%E6%B3%A8%E8%A7%A3%EF%BC%88Repeating-Annotations%EF%BC%89"><span class="toc-number">1.1.5.</span> <span class="toc-text">可重复注解（Repeating Annotations）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Java9"><span class="toc-number">1.2.</span> <span class="toc-text">Java9</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Java%E6%A8%A1%E5%9D%97%E5%8C%96%EF%BC%88Java-Module-System%EF%BC%89"><span class="toc-number">1.2.1.</span> <span class="toc-text">Java模块化（Java Module System）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%A6%E6%9C%89%E5%86%85%E9%83%A8%E5%8C%BF%E5%90%8D%E7%B1%BB%E7%9A%84%E8%8F%B1%E5%BD%A2%E8%AF%AD%E6%B3%95%EF%BC%88Diamond-Syntax-with-Inner-Anonymous-Classes%EF%BC%89"><span class="toc-number">1.2.2.</span> <span class="toc-text">带有内部匿名类的菱形语法（Diamond Syntax with Inner Anonymous Classes）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A7%81%E6%9C%89%E6%8E%A5%E5%8F%A3%E6%96%B9%E6%B3%95%EF%BC%88Private-Interface-Methods%EF%BC%89"><span class="toc-number">1.2.3.</span> <span class="toc-text">私有接口方法（Private Interface Methods）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Java10"><span class="toc-number">1.3.</span> <span class="toc-text">Java10</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F%E7%B1%BB%E5%9E%8B%E6%8E%A8%E6%96%AD%EF%BC%88Local-Variable-Type-Inference%EF%BC%89"><span class="toc-number">1.3.1.</span> <span class="toc-text">局部变量类型推断（Local Variable Type Inference）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Java11"><span class="toc-number">1.4.</span> <span class="toc-text">Java11</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%B8%AD%E7%9A%84%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F%E7%B1%BB%E5%9E%8B%E6%8E%A8%E6%96%AD%EF%BC%88Local-Variable-Type-in-Lambda-Expressions%EF%BC%89"><span class="toc-number">1.4.1.</span> <span class="toc-text">Lambda表达式中的局部变量类型推断（Local Variable Type in Lambda Expressions）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Java14"><span class="toc-number">1.5.</span> <span class="toc-text">Java14</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Switch%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%88Switch-Expressions%EF%BC%89"><span class="toc-number">1.5.1.</span> <span class="toc-text">Switch表达式（Switch Expressions）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Java15"><span class="toc-number">1.6.</span> <span class="toc-text">Java15</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E6%9C%AC%E5%9D%97%EF%BC%88Text-Blocks%EF%BC%89"><span class="toc-number">1.6.1.</span> <span class="toc-text">文本块（Text Blocks）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Java16"><span class="toc-number">1.7.</span> <span class="toc-text">Java16</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#instanceof-%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D%EF%BC%88Pattern-Matching-of-instanceof%EF%BC%89"><span class="toc-number">1.7.1.</span> <span class="toc-text">instanceof 模式匹配（Pattern Matching of instanceof）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A1%A3%E6%A1%88%E7%B1%BB%EF%BC%88Records%EF%BC%89"><span class="toc-number">1.7.2.</span> <span class="toc-text">档案类（Records）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Java17"><span class="toc-number">1.8.</span> <span class="toc-text">Java17</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%81%E9%97%AD%E7%B1%BB%EF%BC%88Sealed-Classes%EF%BC%89"><span class="toc-number">1.8.1.</span> <span class="toc-text">封闭类（Sealed Classes）</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/bd6c95d5/" title="Windows10安装java环境"><img src="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230131095714.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Windows10安装java环境"/></a><div class="content"><a class="title" href="/posts/bd6c95d5/" title="Windows10安装java环境">Windows10安装java环境</a><time datetime="2023-01-31T01:59:34.432Z" title="发表于 2023-01-31 09:59:34">2023-01-31</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/fdc8614/" title="Java8到Java17之间的主要特性描述"><img src="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230130095755.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java8到Java17之间的主要特性描述"/></a><div class="content"><a class="title" href="/posts/fdc8614/" title="Java8到Java17之间的主要特性描述">Java8到Java17之间的主要特性描述</a><time datetime="2023-01-30T01:59:04.252Z" title="发表于 2023-01-30 09:59:04">2023-01-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/9542a1f7/" title="CentOS7安装OpenSSL环境"><img src="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230114172144.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="CentOS7安装OpenSSL环境"/></a><div class="content"><a class="title" href="/posts/9542a1f7/" title="CentOS7安装OpenSSL环境">CentOS7安装OpenSSL环境</a><time datetime="2023-01-14T09:34:08.209Z" title="发表于 2023-01-14 17:34:08">2023-01-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/da432f73/" title="OpenSSL生成证书"><img src="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230114172959.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="OpenSSL生成证书"/></a><div class="content"><a class="title" href="/posts/da432f73/" title="OpenSSL生成证书">OpenSSL生成证书</a><time datetime="2023-01-14T09:33:46.203Z" title="发表于 2023-01-14 17:33:46">2023-01-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/e0523c/" title="CentOS7安装cmake工具"><img src="http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20221217113021.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="CentOS7安装cmake工具"/></a><div class="content"><a class="title" href="/posts/e0523c/" title="CentOS7安装cmake工具">CentOS7安装cmake工具</a><time datetime="2022-12-17T03:39:03.528Z" title="发表于 2022-12-17 11:39:03">2022-12-17</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('http://xiguapengpeng.gitee.io/imgtu/blog/post_cover_20230130095755.png')"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By xiguapengpeng</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">醉后不知天在水，满船清梦压星河。</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><div class="aplayer no-destroy" data-id="60198" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true"></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"log":false});</script></body></html>